<template>
  <div class="game-container">
    <div class="header">
      <div class="status">{{ status }}</div>
      <button @click="resetGame" class="reset-button">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="reset-icon">
          <path d="M3 12a9 9 0 1 1 9 9"></path>
          <path d="M3 12h9"></path>
          <path d="M3 12l4-4"></path>
          <path d="M3 12l4 4"></path>
        </svg>
      </button>
    </div>
    <div class="game-board">
      <div class="board">
        <div v-for="(row, i) in board" :key="i" class="board-row">
          <div v-for="(cell, j) in row" :key="j" 
               class="cell"
               @click="makeMove(i, j)">
            <div v-if="cell !== null" 
                 class="piece"
                 :class="{ 'black': cell === 'black', 'white': cell === 'white' }">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const board = ref(Array(15).fill(null).map(() => Array(15).fill(null)))
const currentPlayer = ref('black')
const winner = ref(null)

const status = computed(() => {
  if (winner.value) {
    return `${winner.value === 'black' ? '黑棋' : '白棋'}获胜！`
  }
  return `${currentPlayer.value === 'black' ? '黑棋' : '白棋'}选手正在下棋`
})

const countInDirection = (row, col, [dx, dy]) => {
  let count = 0
  let x = row + dx
  let y = col + dy
  const player = board.value[row][col]

  while (x >= 0 && x < 15 && y >= 0 && y < 15 && board.value[x][y] === player) {
    count++
    x += dx
    y += dy
  }
  return count
}

const checkWin = (row, col) => {
  const directions = [
    [[0, 1], [0, -1]],  // 水平
    [[1, 0], [-1, 0]],  // 垂直
    [[1, 1], [-1, -1]], // 对角线
    [[1, -1], [-1, 1]]  // 反对角线
  ]

  return directions.some(dir => {
    const count = 1 + countInDirection(row, col, dir[0])
                   + countInDirection(row, col, dir[1])
    return count >= 5
  })
}

const makeMove = (row, col) => {
  if (winner.value || board.value[row][col] !== null) return

  board.value[row][col] = currentPlayer.value

  if (checkWin(row, col)) {
    winner.value = currentPlayer.value
  } else {
    currentPlayer.value = currentPlayer.value === 'black' ? 'white' : 'black'
  }
}

const resetGame = () => {
  board.value = Array(15).fill(null).map(() => Array(15).fill(null))
  currentPlayer.value = 'black'
  winner.value = null
}
</script>

<style scoped>
.game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0;
  min-height: 100vh;
  padding: 20px;
  box-sizing: border-box;
}

.header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
  margin-bottom: 20px;
  height: 60px;
}

.status {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  height: 60px;
  line-height: 60px;
}

.reset-button {
  width: 60px;
  height: 60px;
  padding: 15px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reset-icon {
  width: 24px;
  height: 24px;
}

.reset-button:hover {
  background: #45a049;
  transform: rotate(180deg);
}

.reset-button:active {
  transform: rotate(180deg) scale(0.95);
}

.game-board {
  flex-grow: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.board {
  display: inline-block;
  background: #dcb35c;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
  position: relative;
}

.board-row {
  display: flex;
}

.cell {
  width: 60px;
  height: 60px;
  position: relative;
  cursor: pointer;
}

.cell::before, .cell::after {
  content: '';
  position: absolute;
  background: #000;
  pointer-events: none;
}

.cell::before {
  width: 100%;
  height: 2px;
  top: 50%;
  left: 0;
}

.cell::after {
  width: 2px;
  height: 100%;
  left: 50%;
  top: 0;
}

.piece {
  position: absolute;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  z-index: 1;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.black {
  background: #000;
  box-shadow: 0 4px 8px rgba(0,0,0,0.5);
}

.white {
  background: #fff;
  box-shadow: 0 4px 8px rgba(0,0,0,0.3);
  border: 2px solid #ccc;
}

/* 添加外边框 */
.board::before {
  content: '';
  position: absolute;
  top: 20px;
  left: 40px;
  right: 40px;
  bottom: 40px;
  border: 3px solid #000;
  pointer-events: none;
}

/* 适配iPad横屏和竖屏 */
@media screen and (max-width: 1800px) {
  .game-container {
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }

  .header {
    width: 90%;
    max-width: 600px;
    margin-bottom: 10px;
    background-color: aquamarine;
  }

  .status {
    font-size: 1.8em;
  }

  .reset-button {
    padding: 15px 30px;
    font-size: 1.3em;
  }
}

/* 适配小屏幕设备 */
@media screen and (max-width: 900px) {
  .cell {
    width: 50px;
    height: 50px;
  }

  .piece {
    width: 44px;
    height: 44px;
  }

  .board {
    padding: 30px;
  }

  .board::before {
    top: 30px;
    left: 30px;
    right: 30px;
    bottom: 30px;
  }
}
</style>
